# @Time:   2021/2/5 17:17
# @Author: Zong Wei

import random
from time import sleep
from faker import Faker
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as ec
from selenium.webdriver.support.wait import WebDriverWait

from ui_automation_testing.test.pageObject.loginPage import LoginPage
from ui_automation_testing.utils.logger import Logger


class UNPage(LoginPage):
    logger = Logger(__name__).get_log()
    menu = (By.XPATH, "//ul[@class='site-menu']//span[.='品名管理']")
    sub_menu = (By.XPATH, "//ul[@class='site-menu']//span[.='UN管理']")
    search_button = (By.ID, "lr-search")
    add_button = (By.ID, "lr-add")
    edit_button = (By.ID, "lr-edit")
    del_button = (By.ID, "lr-delete")
    save_button = (By.ID, "lr-save")
    UN_code = (By.ID, "UnNo")
    classDiv = (By.ID, "ClassOrDiv")
    desc = (By.ID, "StowageAndObservate")
    sg = (By.ID, "ExtendSegregate")
    ems = (By.ID, "EMS")
    confirm_button = (By.XPATH, "//a[.='确认']")
    first_row = (By.XPATH, "//span[.='MFAG']//following::tr[1]")

    def __init__(self):
        self.driver = LoginPage().login()
        self.wait = WebDriverWait(self.driver, 10)

    def un_home(self):
        self.logger.info("登录系统成功")
        self.wait.until(ec.element_to_be_clickable(self.menu)).click()
        self.wait.until(ec.element_to_be_clickable(self.sub_menu)).click()
        sleep(1)
        return self.driver

    def add_un(self, code='9999', div='8.8'):
        self.driver.switch_to.frame(1)
        self.wait.until(ec.element_to_be_clickable(self.add_button)).click()
        self.logger.info("添加UN")
        sleep(2)
        self.driver.switch_to.default_content()
        self.driver.switch_to.frame(2)
        sleep(1)
        self.wait.until(ec.presence_of_element_located(self.UN_code)).send_keys(code)
        self.wait.until(ec.presence_of_element_located(self.classDiv)).send_keys(div)
        sleep(1)
        self.wait.until(ec.presence_of_element_located(self.sg)).send_keys(str(format(Faker().latitude(), '.4f')))
        self.wait.until(ec.presence_of_element_located(self.desc)).send_keys(Faker().address())
        sleep(1)
        self.wait.until(ec.element_to_be_clickable(self.save_button)).click()
        sleep(1)
        self.logger.info("新增完成")

    def search_un(self, code='9999', div='8.8'):
        self.driver.switch_to.frame(1)
        self.wait.until(ec.presence_of_element_located(self.UN_code)).send_keys(code)
        self.wait.until(ec.presence_of_element_located(self.classDiv)).send_keys(div)
        sleep(1)
        self.wait.until(ec.element_to_be_clickable(self.search_button)).click()
        sleep(2)
        self.logger.info("查询完成")

    def edit_un(self):
        self.search_un()
        self.wait.until(ec.element_to_be_clickable(self.first_row)).click()
        sleep(1)
        self.wait.until(ec.element_to_be_clickable(self.edit_button)).click()
        sleep(1)
        self.driver.switch_to.default_content()
        self.driver.switch_to.frame(2)
        self.wait.until(ec.presence_of_element_located(self.ems)).clear()
        self.wait.until(ec.presence_of_element_located(self.ems)).send_keys('F-B,S-Y')
        sleep(1)
        self.wait.until(ec.element_to_be_clickable(self.save_button)).click()
        self.logger.info("编辑修改完成")

    def del_un(self):
        self.search_un()
        self.wait.until(ec.element_to_be_clickable(self.first_row)).click()
        sleep(1)
        self.wait.until(ec.element_to_be_clickable(self.del_button)).click()
        sleep(1)
        self.driver.switch_to.default_content()
        self.wait.until(ec.element_to_be_clickable(self.confirm_button)).click()
        self.logger.info("删除成功")


if __name__ == '__main__':
    pg = UNPage()
    pg.un_home()
    pg.del_un()

